<!-- The exported code uses Tailwind CSS. Install Tailwind CSS in your dev environment to ensure all styles work. -->

<template>
  <div class="p-6">
    <!-- 头部导航 -->
    <div class="flex items-center mb-6">
      <el-icon class="text-gray-500 mr-2"><Back /></el-icon>
      <h1 class="text-xl font-medium">订单管理</h1>
      <div class="ml-auto text-sm text-gray-500">最近更新：2025-05-04 14:30</div>
      <el-button type="primary" size="small" class="ml-4 !rounded-button whitespace-nowrap"
        >刷新</el-button
      >
    </div>

    <!-- 搜索区域 -->
    <div class="bg-white p-5 rounded-lg shadow-sm mb-6">
      <div class="flex flex-wrap items-center gap-4">
        <div class="w-64">
          <div class="relative" style="margin-top: 20px;">
            <input
              v-model="searchText"
              type="text"
              placeholder="订单号"
              class="w-full pl-9 pr-3 py-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-sm"
            />
            <el-icon style="bottom: 18px;" class="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400"
              ><Search
            /></el-icon>
          </div>
        </div>

        <div class="w-64">
          <div class="relative" style="margin-top: 20px;">
            <input
              v-model="productName"
              type="text"
              placeholder="商品名称"
              class="w-full pl-9 pr-3 py-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-sm"
            />
            <el-icon style="bottom: 18px;" class="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400"
              ><Search
            /></el-icon>
          </div>
        </div>

        <div class="flex items-center">
          <span class="text-sm text-gray-600 mr-2">下单时间</span>
          <div class="flex items-center">
            <el-date-picker
              v-model="startDate"
              type="date"
              placeholder="开始日期"
              size="default"
              class="w-40"
            />
            <span class="mx-2">至</span>
            <el-date-picker
              v-model="endDate"
              type="date"
              placeholder="结束日期"
              size="default"
              class="w-40"
            />
          </div>
        </div>

        <div class="flex items-center">
          <span class="text-sm text-gray-600 mr-2">支付状态</span>
          <el-select v-model="paymentStatus" placeholder="全部" class="w-40">
            <el-option label="全部" value="100" />
            <el-option label="已支付" value="1" />
            <el-option label="待支付" value="-1" />
          </el-select>
        </div>

        
        

        <div class="flex items-center ml-auto">
          <el-button plain size="default" class="mr-2 !rounded-button whitespace-nowrap">
            <el-icon class="mr-1"><Refresh /></el-icon> 重置
          </el-button>
          <el-button @click="getPageList"  type="primary" size="default" class="!rounded-button whitespace-nowrap">
            <el-icon class="mr-1"><Search /></el-icon> 搜索
          </el-button>
        </div>
      </div>
    </div>

    <!-- 订单列表 -->
    <div class="bg-white rounded-lg shadow-sm">
      <div class="p-4 flex items-center justify-between border-b">
        <div class="text-base font-medium">
          订单列表 <span class="text-gray-500 text-sm ml-2">共 {{total}} 条</span>
        </div>
        <div class="flex items-center">
          <el-button type="primary" class="mr-2 !rounded-button whitespace-nowrap">
            <el-icon class="mr-1"><Download /></el-icon> 导出订单
          </el-button>
          <el-dropdown>
            <el-button class="!rounded-button whitespace-nowrap">
              批量操作 <el-icon class="ml-1"><ArrowDown /></el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>批量取消</el-dropdown-item>
                <el-dropdown-item>批量删除</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>

      <!-- 表格 -->
      <el-table :data="orderList" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column label="订单号" prop="orderNumber" min-width="180" sortable>
          <template #default="scope">
            <span class="text-blue-500">{{ scope.row.orderNumber }}</span>
          </template>
        </el-table-column>
        <el-table-column label="下单时间" prop="orderTime" min-width="180" sortable />
        <el-table-column label="商品信息" min-width="280">
          <template #default="scope">
            <div class="flex items-center">
              <div class="w-10 h-10 rounded overflow-hidden mr-3">
                <img
                  src="https://p0.ssl.img.360kuai.com/dmfd/__60/t01cd8f58013e6f6ced.jpg"
                  alt="商品图片"
                  class="w-full h-full object-cover"
                />
              </div>
              <div>
                <div class="text-sm">{{ scope.row.productList?.[0].menu.name}}</div>
                <div class="text-xs text-gray-500">
                  <el-popover
      :width="300"
      popper-style="box-shadow: rgb(14 18 22 / 35%) 0px 10px 38px -10px, rgb(14 18 22 / 20%) 0px 10px 20px -15px; padding: 20px;"
    >
      <template #reference>
        <el-button type="success">展示更多</el-button>
      </template>
      <template #default>
        <ul  class="infinite-list" style="overflow: auto">
        <li v-for="(item,index) in scope.row.productList" :key="item.menu.id" class="infinite-list-item"><div class="goodCart" >
          <div class="good-img">
            <img src="https://p0.ssl.img.360kuai.com/dmfd/__60/t01cd8f58013e6f6ced.jpg" width="120px" height="50px">
          </div>
          <div class="good-content">
             <div class="good-title">{{ item.menu.name }}</div>
             <div class="good-count">数量：{{item.count}}</div>
             <div class="good-price">价格：2121</div>
          </div>
        </div></li>
      </ul>
        
      </template>
    </el-popover>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="订单金额" prop="amount" min-width="120" sortable>
          <template #default="scope">
            <span class="font-medium">¥{{ scope.row.amount }}</span>
          </template>
        </el-table-column>
        <el-table-column label="支付状态" min-width="120">
          <template #default="scope">
            <el-tag
              :type="scope.row.paymentStatus === '已支付' ? 'success' : scope.row.paymentStatus === '待支付' ? 'warning' : 'info'"
              size="small"
              effect="plain"
            >
              {{ scope.row.paymentStatus }}
            </el-tag>
          </template>
        </el-table-column>
        
        <el-table-column label="操作" min-width="180" fixed="right">
          <template #default="scope">
            <!-- <el-button type="primary" size="small" plain class="!rounded-button whitespace-nowrap">
              详情
            </el-button> -->
            <el-button
              v-if="scope.row.paymentStatus === '待支付'"
              type="success"
              size="small"
              plain
              class="ml-2 !rounded-button whitespace-nowrap"
              @click="goPay(scope.row.orderNumber)"
            >
              去支付
            </el-button>
            <el-button
              type="danger"
              size="small"
              plain
              class="ml-2 !rounded-button whitespace-nowrap"
              @click="deleteOrder(scope.row.orderNumber)"
            >
              删除
            </el-button>
            
            
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="p-4 flex justify-end">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import {postPageOrderListService} from '@/api/order'
import {
  User,
  Lock,
  Setting,
  ChatDotRound,
  PictureFilled,
  VideoPlay,
  Medal,
  Star,
  Goods,
  Document,
  Notebook,
  Upload,
  Back,
  Search,
  Refresh,
  Download,
  ArrowDown,
} from '@element-plus/icons-vue'

import { useRouter1 } from '../utils/useRoute'
import { formatDate } from '@/utils/dateFormat';
import {getOrderService,postDeleteOrderService} from '@/api/order'
import { ElMessage } from 'element-plus';
import router from '@/router';
const {
  goToHome,
  goToDocumentUpload,
  goToKnowledgeBaseManagement,
  goToMemberCenter,
  goToNotificationSettings,
  goToOrder,
  goToPointsManagement,
  goToSecuritySettings,
  goToUser,
  goToAiChat,
  goToAiDrawing,
  goToProduct,
} = useRouter1()
// 搜索条件
const startDate = ref('')
const endDate = ref('')
const paymentStatus = ref('100')

const searchText=ref('')
// 分页
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(28)
const productName=ref('')
// 选中的订单
const selectedOrders = ref([])
// 处理选择变化
const handleSelectionChange = (selection: any[]) => {
  selectedOrders.value = selection
}

const handleSizeChange=()=>{
  getPageList()
}

const handleCurrentChange=()=>{
  getPageList()
}

const deleteOrder=async(orderNo)=>{
  const res=await postDeleteOrderService(orderNo)
  getPageList()
  ElMessage.success('删除成功')
}

const getPageList=async()=>{
  const res=await postPageOrderListService({
    productName: productName.value===''?null:productName.value,
    orderNo: searchText.value===''?null:searchText.value,
    startTime: new Date(startDate.value).getTime(),
    endTime: new Date(endDate.value).getTime(),
    status: paymentStatus.value==='100'?null:Number(paymentStatus.value),
    page:{
      page:currentPage.value,
      limit:pageSize.value
    }
  })
  total.value=res.totalSize
  orderList.value=res.orderList.map(item=>{
    let status='';
    if(item.order.status===1){
      status='已支付'
    }
    if(item.order.status===-1){
      status='待支付'
    }
    if(item.order.status===0){
      status='已关闭'
    }
    
    return {
      id:String(item.order.id),
      orderNumber: String(item.orderNo),
      orderTime: formatDate(item.order.createTime),
      productList:item.goodsList,
      amount:String(item.order.amount/100),
      paymentStatus:status ,
    }
  })
  
}
getPageList()

const goPay=async(orderNo)=>
{
  const res=await getOrderService(orderNo)
  console.log(res.orderNo)
  console.log(res.order.codeUrl)
  console.log(res.order.amount)
  localStorage.setItem('orderId',res.orderNo)
  localStorage.setItem('payCode',res.order.codeUrl)
  localStorage.setItem('amount',res.order.amount/100)
  ElMessage({
    message: '正在跳转支付页面...',
    type: 'success',
  })
  router.push('/pay-code')
}
// 模拟订单数据
const orderList = ref([
  
])
</script>

<style scoped>
:deep(.el-table .cell) {
  padding: 12px 8px;
}

:deep(.el-button) {
  font-size: 0.875rem;
}

:deep(.el-input__wrapper) {
  box-shadow: 0 0 0 1px #dcdfe6 inset;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 0 0 1px #c0c4cc inset;
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 1px #409eff inset;
}

:deep(.el-table th.el-table__cell) {
  background-color: #f5f7fa;
}

:deep(.el-pagination) {
  justify-content: flex-end;
}

:deep(.el-select .el-input__wrapper) {
  padding-right: 30px;
}

:deep(.el-date-editor .el-input__wrapper) {
  padding-left: 38px;
}

:deep(.el-table--enable-row-hover .el-table__body tr:hover > td.el-table__cell) {
  background-color: #f5f7fa;
}

.goodCart{
  display: flex;
  justify-content: space-between;
  padding: 10px;
  .good-img{
    flex: 6;
  }
  .good-content{
    .good-title{
      font-size: 14px;
      font-weight: 700;
    }
    .good-price{
      color: coral;
    }
    flex: 4;
  }
}
.infinite-list {
  height: 200px;
  padding: 0;
  margin: 0;
  list-style: none;
}
</style>
